<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/7/18
 * Time: 13:43
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;


use app\common\lib\Status;
use think\Model;

class Category extends BaseModel
{
    protected $autoWriteTimestamp = true;

    /**
     * 查询标题是否存在
     * @param string $title
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCategoryByIsTitle(string $title = '')
    {
        return $this->where('title', $title)
            ->where('status', "<>", Status::Mysql_delete)
            ->find();
    }

    /**
     * 获取状态正常的分类
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCateTree(string $filed = '*')
    {
        return $this->field($filed)
            ->where('status', Status::Mysql_success)
            ->select()
            ->toArray();
    }

    /**
     * 根据pid查询所有数据
     * @param int $pid
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCategoryByPid(int $pid = 0, string $field = "*", string $orderBy = "id desc")
    {
        return $this->field($field)
            ->where('pid', $pid)
            ->where('status', Status::Mysql_success)
            ->order($orderBy)
            ->select()
            ->toArray();
    }


}